/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.mallow.impl.model.vot;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jiscinvolve.astrodabis.mallow.model.tap.TapTable;

/**
 * Simple implementation of a VOTable JoinTagParser, using fixed tables.
 *
 */
public class SimpleVOTableJoinTagParser
extends VOTableJoinTagParser
    {

    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(SimpleVOTableJoinTagParser.class);

    /**
     * The left hand TapTable.
     *
     */
    private TapTable t1 ;

    /**
     * The right hand TapTable.
     *
     */
    private TapTable t2 ;

    /**
     * Public constructor.
     *
     */
    public SimpleVOTableJoinTagParser(TapTable t1, TapTable t2, InputStream src)
        {
        super(src);
        this.t1 = t1 ;
        this.t2 = t2 ;
        }

// JoinTagData methods

    @Override
    public TapTable t1()
        {
        return this.t1 ;
        }

    @Override
    public TapTable t2()
        {
        return this.t2 ;
        }

    @Override
    public String p1()
        {
        if (data != null)
            {
            if (data.length > 0)
                {
                Object object = data[0] ;
                if (object != null)
                    {
                    return object.toString() ;
                    }
                }
            }
        return null ;
        }

    @Override
    public String p2()
        {
        if (data != null)
            {
            if (data.length > 1)
                {
                Object object = data[1] ;
                if (object != null)
                    {
                    return object.toString() ;
                    }
                }
            }
        return null ;
        }

    @Override
    public String name()
        {
        if (data != null)
            {
            if (data.length > 2)
                {
                Object object = data[2] ;
                if (object != null)
                    {
                    return object.toString() ;
                    }
                }
            }
        return null ;
        }

    @Override
    public Double value()
        {
        if (data != null)
            {
            if (data.length > 3)
                {
                Object object = data[3] ;
                if (object != null)
                    {
                    if (object instanceof Double)
                        {
                        return (Double) object ;
                        }
                    else if (object instanceof Number)
                        {
                        return ((Number)object).doubleValue() ;
                        }
                    else {
                        String string = object.toString();
                        try {
                            return Double.valueOf(
                                string
                                );
                            }
                        catch (NumberFormatException ouch)
                            {
                            log.error("NumberFormatException while processing value [" + string + "]");
                            }
                        }
                    }
                }
            }
        return null ;
        }
    }

